package com.jbpm.websercvice.manager.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.emms.common.bean.PageBean;
import com.emms.common.constant.CommonEnumType.DisabledAndEnable;
import com.emms.common.dao.NamedQueryBaseDao;
import com.emms.common.exception.FrameworkException;
import com.emms.common.util.BaseUtil;
import com.emms.controller.baseinfo.vo.CodeInfoBean;
import com.emms.controller.system.vo.SysOrgBean;
import com.jbpm.extjsvo.JSONTreeNode;
import com.jbpm.websercvice.manager.VCurrencyService;
import com.jbpm.websercvice.vo.ProjectInfoWfBean;
import com.jbpm.websercvice.vo.SectionWfBean;
import com.jbpm.websercvice.vo.SysRoleBean;
import com.jbpm.websercvice.vo.SysuserBean;
import com.jbpm.websercvice.vo.WFSystemBean;
import com.jbpm.websercvice.vo.WfModuleBean;

/**
 * 通用查询
 * @author luomj
 *
 */
@Service(value="vCurrencyServiceImpl")
public class VCurrencyServiceImpl implements VCurrencyService {

	
	@Resource(name="namedQueryBaseDao")
	private NamedQueryBaseDao<Object> namedQueryBaseDao;
	
	
	/**
	 * 查询总条数
	 * @param vb
	 * @return
	 * @throws FrameworkException
	 */
	private int count(Object vb,String sqlKey) throws FrameworkException{
		return namedQueryBaseDao.getTotalRows(sqlKey, vb);
	}

	/**
	 * 查询总内容
	 * @param vb
	 * @param pb
	 * @return
	 * @throws FrameworkException
	 */
	private List<Object> list(Object vb, PageBean pb,String sqlKey) throws FrameworkException{
		if(!BaseUtil.isEmpty(pb)){
			return namedQueryBaseDao.findAllEntitiesByCondition(sqlKey, vb, pb.getStartRow(), pb.getEndRow());
		}else{
			return namedQueryBaseDao.findAllEntitiesByCondition(sqlKey, vb);
		}
		
	}

	/**
	 * 委托角色选择数据获取
	 * @param model
	 * @param webServiceBean
	 * @param pb
	 * @return
	 * @throws FrameworkException
	 */
	public PageBean getRoleByParam(SysRoleBean vb, PageBean pb)
			throws FrameworkException {
		pb.setTotalProperty(count(vb,"webService.getRoleByParamCount"));
		pb.setRoot(list(vb,pb,"webService.getRoleByParamList"));
		return pb;
	}

	/**
	 * 委托用户选择数据获取
	 * 
	 * @param model
	 * @param webServiceBean
	 * @param pb
	 * @return
	 * @throws FrameworkException
	 */
	public PageBean getUserByParam(SysuserBean vb, PageBean pb)
			throws FrameworkException {
		pb.setTotalProperty(count(vb,"webService.getUseByParamCount"));
		pb.setRoot(list(vb,pb,"webService.getUseByParamList"));
		return pb;
	}

	/**
	 * 所属系统数据获取
	 * @param model
	 * @param wFSystemBean
	 * @param pb
	 * @return
	 * @throws FrameworkException
	 */
	public PageBean getSystemByParam(WFSystemBean vb, PageBean pb)
			throws FrameworkException {
		pb.setTotalProperty(count(vb,"webService.getSystemByParamCount"));
		pb.setRoot(list(vb,pb,"webService.getSystemByParamList"));
		return pb;
	}

	/**
	 * 所属项目数据获取
	 * 
	 * @param model
	 * @param projectInfoWfBean
	 * @param pb
	 * @return
	 * @throws FrameworkException
	 */
	public PageBean getProjectByParam(ProjectInfoWfBean vb, PageBean pb) throws FrameworkException {
		pb.setTotalProperty(count(vb,"webService.getProjectByParamCount"));
		pb.setRoot(list(vb,pb,"webService.getProjectByParamList"));
		return pb;
	}

	/**
	 * 所属标段数据获取
	 * 
	 * @param model
	 * @param projectInfoWfBean
	 * @param pb
	 * @return
	 * @throws FrameworkException
	 */
	public PageBean getSectionByParam(SectionWfBean vb, PageBean pb)
			throws FrameworkException {
		pb.setTotalProperty(count(vb,"webService.getSectionByParamCount"));
		pb.setRoot(list(vb,pb,"webService.getSectionByParamList"));
		return pb;
	}

	/**
	 * 所属业务模块数据获取
	 * 
	 * @param model
	 * @param wFModuleBean
	 * @param pb
	 * @return
	 * @throws FrameworkException
	 */
	public PageBean getModuleByParam(WfModuleBean vb, PageBean pb)
			throws FrameworkException {
		pb.setTotalProperty(count(vb,"webService.getModuleByParamCount"));
		pb.setRoot(list(vb,pb,"webService.getModuleByParamList"));
		return pb;
	}

	/**
	 * 获取所以组织类型信息
	 * @param cb
	 * @param pb
	 * @return
	 * @throws FrameworkException
	 */
	public List<Object> getLoadOrgTreeIndex(CodeInfoBean cb, PageBean pb)
			throws FrameworkException {
		return list(cb, null, "webService.webCodeInfoList");
	}

	//获取子节点信息
	public JSONTreeNode getLoadOrgBuildTreeModel(JSONTreeNode node, String id) throws FrameworkException {
		if(BaseUtil.isEmpty(node))return null;
		JSONTreeNode n=node;
		n=createTreeChildren(n);
		return n;
	}

	public JSONTreeNode createTreeChildren(JSONTreeNode root) throws FrameworkException{
		//获取子节点
		SysOrgBean s=new SysOrgBean();
		s.setPid(root.getId());
		s.setState(DisabledAndEnable.enable.getValue());
		List<Object> createTreeOrgList=list(s, null, "webService.webSysOrgTreeList");
		//孩子节点
		//String parentId=root.getId();
		//孩子节点信息
		List<JSONTreeNode> childrenList=new ArrayList<JSONTreeNode>();
		for(Object org:createTreeOrgList){
			   SysOrgBean o=(SysOrgBean) org;
				//节点
				JSONTreeNode node=new JSONTreeNode();
				//节点id
				node.setId(o.getId());
				//节点名称
				node.setText(o.getName());
				//编码
				node.setCode(o.getOrgCode());
				//提示信息
				node.setDescription(o.getRemark());      
				//父节点
				node.setParent(root.getId());
				//获取子节点信息
				childrenList.add(node);
				//获取下个子节点信息
				node=createTreeChildren(node);
				//是否叶子
				if(node.getChildren().size()>0){
					node.setLeaf(false);
				}else{
					node.setLeaf(true);
				}
				//是否展开
				node.setExpandable(true);
		}
	 if(childrenList.size()>0){
		 root.setChildren(childrenList);
	 }
	 return root;
	}
}
